
import { themeList, tabBarList } from './theme.js';
// 默认配置
import { t } from '@/common/sju.i18n.js'

// 常量定义
const CONSTANTS = {
    DEFAULT_THEME: '蓝色主题',
  }

/**
 * @description 动态设置tabBar
 * @param {String}
 */
function setTabBar() {
    // const theme = useThemeStore()
    let themeName = uni.getStorageSync('themeName')
    let themeColor = uni.getStorageSync('theme')
    // 获取默认主题
    const defaultTheme = themeList.find(
      item => item.name === CONSTANTS.DEFAULT_THEME
    )
    
    // 如果没有存储的主题，使用默认主题
    if (!themeName || !themeColor) {
      useThemeStore().setTheme(defaultTheme)
      themeName = defaultTheme.name
      themeColor = defaultTheme.value.find(item => item.name === '--primary-color')?.value || '#48b8d0'
    }
  
    // 查找对应的tabBar配置
    const tabBarConfig = tabBarList.find(item => item.themeName === themeName)
    if (!tabBarConfig) {
      console.warn(`未找到主题${themeName}对应的tabBar配置`)
      return
    }
  
    try {
      // 设置tabBar样式
      uni.setTabBarStyle({
        selectedColor: themeColor.split(/\;|\:/)[1] || '#48b8d0',
        borderStyle: 'white'
      })
      
      // 设置tabBar项
      uni.setTabBarItem({
        index: 0,
        text: t('tabbar.index'),
        iconPath: tabBarConfig.homeicon,
        selectedIconPath: tabBarConfig.home
      })
  
      uni.setTabBarItem({
        index: 1,
        text: t('tabbar.user'),
        iconPath: tabBarConfig.usericon,
        selectedIconPath: tabBarConfig.user
      })
    } catch (error) {
      console.error('设置tabBar失败:', error)
    }
  }

export {
	setTabBar
}